package edu.msoe.se3811.jmlunit.code.isqrt;

public class IntegerSquareRoot {
	
	/**
	 * @param x value for which square root is desired
	 * @return calculated square root value
	 */

	/*@
	  @ public normal_behavior
	  @ requires x >= 0;
	  @ ensures \result * \result <= x;
	  @ ensures (\result * \result) > x - 2*(\result)-1;
	  @ assignable \nothing;
	  @ also
	  @ public exceptional_behavior
	  @ requires x < 0;
	  @ signals_only IllegalArgumentException;
	  @ assignable \nothing;
	  @*/
	public static int iSqrt (int x) {
		
		double dResult =  0.0;
		if (x < 0) {
			throw new IllegalArgumentException("Arg negative = "+x);
		}
		dResult =  Math.sqrt((double) x);
//		System.out.println("dResult ("+x+") = "+dResult);
		return (int) dResult;
	}

//	/**
//	 * @param args not used
//	 */
//	public static void main(String[] args) {
//		for (int i = 1; i <= 20; i++) {
//			doTestCase(i);
//		}
//		doTestCase(-1);
//		doTestCase(-10);
////		doTestCase(Integer.MAX_VALUE);
//	}
	
	
	
//	private static void doTestCase (int x) {
//		try {
//			int sqrtVal = iSqrt(x);
//			System.out.println ("iSqrt("+x+") = "+sqrtVal);
//		} catch (IllegalArgumentException e) {
//			System.out.println ("Caught exception: "+e.getMessage());
//		}
//		
//	}

}
